package com.vibration.web.controller.baseinfo;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import ComputeMoreTemOne.computeMoreTempOne;
import com.mathworks.toolbox.javabuilder.MWClassID;
import com.mathworks.toolbox.javabuilder.MWException;
import com.mathworks.toolbox.javabuilder.MWNumericArray;
import com.vibration.common.config.VibrationConfig;
import com.vibration.framework.web.domain.server.Sys;
import com.vibration.socket.domain.DataCollection;
import com.vibration.socket.util.SocketUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.vibration.common.annotation.Log;
import com.vibration.common.core.controller.BaseController;
import com.vibration.common.core.domain.AjaxResult;
import com.vibration.common.enums.BusinessType;
import com.vibration.baseinfo.domain.CzMoreTemperatureData;
import com.vibration.baseinfo.service.ICzMoreTemperatureDataService;
import com.vibration.common.utils.poi.ExcelUtil;
import com.vibration.common.core.page.TableDataInfo;

/**
 * 多点位温度Controller
 * 
 * @author Mr_Wu
 * @date 2023-01-31
 */
@RestController
@RequestMapping("/baseinfo/moreTemperatureData")
public class CzMoreTemperatureDataController extends BaseController
{
    @Autowired
    private ICzMoreTemperatureDataService czMoreTemperatureDataService;

    /**
     * 查询多点位温度列表
     */
    @PreAuthorize("@ss.hasPermi('baseinfo:moreTemperatureData:list')")
    @GetMapping("/list")
    public TableDataInfo list(CzMoreTemperatureData czMoreTemperatureData)
    {
        startPage();
        List<CzMoreTemperatureData> list = czMoreTemperatureDataService.selectCzMoreTemperatureDataList(czMoreTemperatureData);
        return getDataTable(list);
    }

    /**
     * 查询位温度图表数据--计算页用
     */
    @GetMapping("/getTemperatureChartData")
    public AjaxResult getTemperatureChartData(CzMoreTemperatureData czMoreTemperatureData)
    {
        AjaxResult result = czMoreTemperatureDataService.getTemperatureChartData(czMoreTemperatureData);
        return result;
    }

    /**
     * 查询近20条位温度图表数据--首页用
     */
    @GetMapping("/getTemperatureChartDataIndex")
    public AjaxResult getTemperatureChartDataIndex(@RequestParam("equipmentCode") String equipmentCode,@RequestParam("type") Integer type)
    {
        AjaxResult result = czMoreTemperatureDataService.getTemperatureChartDataIndex(equipmentCode,type);
        return result;
    }
    @GetMapping("/getLastTemp/{equipmentCode}")
    public AjaxResult getLastTemp(@PathVariable("equipmentCode") String equipmentCode)
    {
        AjaxResult result = AjaxResult.success();
        Map<String, DataCollection> map = SocketUtil.gyroscopeSocketMap;
        if(map != null){
            DataCollection d = map.get(equipmentCode);
            if(d != null){
                CzMoreTemperatureData cmtd= d.getMoreTemperatureData();
                return result.put("data",cmtd);
            }
        }
        return result.put("data",null);
    }


    @PostMapping("/calculation")
    public AjaxResult calculation(@RequestBody CzMoreTemperatureData czMoreTemperatureData) throws MWException {
        System.err.println("得到K值"+czMoreTemperatureData);
        //查询所有温度数据
        List<CzMoreTemperatureData> temperatureDataList=czMoreTemperatureDataService.calculation(czMoreTemperatureData);
        //获取checkList截取数据
        List<String> subComputeList=czMoreTemperatureData.getCheckList();
        //参数一集合
        List<Double> dataT1=new ArrayList<>();
        //参数二集合
        List<Double> dataT2=new ArrayList<>();
        String tempComputeT1=subComputeList.get(0).substring(0,subComputeList.get(0).lastIndexOf("m"));
            if("5".equals(tempComputeT1)){
                for (int i=0;i<temperatureDataList.size();i++
                ) {
                    Double temperature5 = temperatureDataList.get(i).getTemperature5();
                    dataT1.add(temperature5);
                }
            }else if("10".equals(tempComputeT1)){
                for (int i=0;i<temperatureDataList.size();i++
                ) {
                    Double temperature10 = temperatureDataList.get(i).getTemperature10();
                    dataT1.add(temperature10);
                }
            }else if("15".equals(tempComputeT1)){
                for (int i=0;i<temperatureDataList.size();i++
                ) {
                    Double temperature15 = temperatureDataList.get(i).getTemperature15();
                    dataT1.add(temperature15);
                }
            }else if("20".equals(tempComputeT1)){
                for (int i=0;i<temperatureDataList.size();i++
                ) {
                    Double temperature20 = temperatureDataList.get(i).getTemperature20();
                    dataT1.add(temperature20);
                }
            }else if("25".equals(tempComputeT1)){
                for (int i=0;i<temperatureDataList.size();i++
                ) {
                    Double temperature25 = temperatureDataList.get(i).getTemperature25();
                    dataT1.add(temperature25);
                }
            }else if("30".equals(tempComputeT1)){
                for (int i=0;i<temperatureDataList.size();i++
                ) {
                    Double temperature30 = temperatureDataList.get(i).getTemperature30();
                    dataT1.add(temperature30);
                }
            }else if("35".equals(tempComputeT1)){
                for (int i=0;i<temperatureDataList.size();i++
                ) {
                    Double temperature35 = temperatureDataList.get(i).getTemperature35();
                    dataT1.add(temperature35);
                }
            }else if("40".equals(tempComputeT1)){
                for (int i=0;i<temperatureDataList.size();i++
                ) {
                    Double temperature40 = temperatureDataList.get(i).getTemperature40();
                    dataT1.add(temperature40);
                }
            }else if("45".equals(tempComputeT1)){
                for (int i=0;i<temperatureDataList.size();i++
                ) {
                    Double temperature45 = temperatureDataList.get(i).getTemperature45();
                    dataT1.add(temperature45);
                }
            }else if("50".equals(tempComputeT1)){
                for (int i=0;i<temperatureDataList.size();i++
                ) {
                    Double temperature50 = temperatureDataList.get(i).getTemperature50();
                    dataT1.add(temperature50);
                }
            }
        String tempComputeT2=subComputeList.get(1).substring(0,subComputeList.get(1).lastIndexOf("m"));
        if("5".equals(tempComputeT2)){
            for (int i=0;i<temperatureDataList.size();i++
            ) {
                Double temperature5 = temperatureDataList.get(i).getTemperature5();
                dataT2.add(temperature5);
            }
        }else if("10".equals(tempComputeT2)){
            for (int i=0;i<temperatureDataList.size();i++
            ) {
                Double temperature10 = temperatureDataList.get(i).getTemperature10();
                dataT2.add(temperature10);
            }
        }else if("15".equals(tempComputeT2)){
            for (int i=0;i<temperatureDataList.size();i++
            ) {
                Double temperature15 = temperatureDataList.get(i).getTemperature15();
                dataT2.add(temperature15);
            }
        }else if("20".equals(tempComputeT2)){
            for (int i=0;i<temperatureDataList.size();i++
            ) {
                Double temperature20 = temperatureDataList.get(i).getTemperature20();
                dataT2.add(temperature20);
            }
        }else if("25".equals(tempComputeT2)){
            for (int i=0;i<temperatureDataList.size();i++
            ) {
                Double temperature25 = temperatureDataList.get(i).getTemperature25();
                dataT2.add(temperature25);
            }
        }else if("30".equals(tempComputeT2)){
            for (int i=0;i<temperatureDataList.size();i++
            ) {
                Double temperature30 = temperatureDataList.get(i).getTemperature30();
                dataT2.add(temperature30);
            }
        }else if("35".equals(tempComputeT2)){
            for (int i=0;i<temperatureDataList.size();i++
            ) {
                Double temperature35 = temperatureDataList.get(i).getTemperature35();
                dataT2.add(temperature35);
            }
        }else if("40".equals(tempComputeT2)){
            for (int i=0;i<temperatureDataList.size();i++
            ) {
                Double temperature40 = temperatureDataList.get(i).getTemperature40();
                dataT2.add(temperature40);
            }
        }else if("45".equals(tempComputeT2)){
            for (int i=0;i<temperatureDataList.size();i++
            ) {
                Double temperature45 = temperatureDataList.get(i).getTemperature45();
                dataT2.add(temperature45);
            }
        }else if("50".equals(tempComputeT2)){
            for (int i=0;i<temperatureDataList.size();i++
            ) {
                Double temperature50 = temperatureDataList.get(i).getTemperature50();
                dataT2.add(temperature50);
            }
        }
        //k值
        Double K= czMoreTemperatureData.getK();
        //高度
        Double H=czMoreTemperatureData.getH();
        Double m=2.9;
        //参数一深度
        Double D1=Double.parseDouble(tempComputeT1);
        //参数二深度
        Double D2=Double.parseDouble(tempComputeT2);
        //传参
        List<Double> TxDataList=new ArrayList<>();
        List<Double> qDataList=new ArrayList<>();
        for (int i=0;i<dataT1.size();i++
             ) {
            double getTx=dataT2.get(i)+K*((dataT2.get(i)-dataT1.get(i))/(D2-D1))*H/m;
            double qData=K*((dataT2.get(i)-dataT1.get(i))/(D2-D1));
            qDataList.add(qData);
            TxDataList.add(getTx);
        }
        double[] T1List=new double[dataT1.size()];
        double[] T2List=new double[dataT1.size()];
        //计算的Tx
        double[] computeTxList=new double[dataT1.size()];
        //计算的q
        double[] computeQList=new double[dataT1.size()];
        NumberFormat nf = NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        for (int j = 0; j < dataT1.size(); j++) {
            T1List[j] = dataT1.get(j);
            T2List[j] = dataT2.get(j);
            computeTxList[j] = TxDataList.get(j);
            computeQList[j]=qDataList.get(j);
        }
//        for (int l=0;l<computeTxList.length;l++){
//            System.out.println("得到Tx"+computeTxList[l]);
//        }
        double[][] getAllMoreTempOneData=new double[T1List.length][8];
        //把一维数组合并为二维
        for(int t=0;t<T1List.length;t++){
            //T1值
            getAllMoreTempOneData[t][0]=T1List[t];
            //T2值
            getAllMoreTempOneData[t][1]=T2List[t];
            //Q值
            getAllMoreTempOneData[t][2]=Double.parseDouble(String.format("%.15f", Double.parseDouble(nf.format(computeQList[t]))));
            //计算K值
            getAllMoreTempOneData[t][3]=K;

            getAllMoreTempOneData[t][4]=m;
            //高度H
            getAllMoreTempOneData[t][5]=H;
            //计算Tx值
            getAllMoreTempOneData[t][6]=computeTxList[t];

            //T2-T1
            getAllMoreTempOneData[t][7]=T2List[t]-T1List[t];
        }

        String moreTempOneComputePathName= VibrationConfig.getMoreTempPath()+"/"+ czMoreTemperatureData.getEquipmentCode()+"/"+"moreTempComputeOne.txt";
        String descPath=VibrationConfig.getMoreTempPath()+"/"+ czMoreTemperatureData.getEquipmentCode();
        if(!new File(descPath).exists()){
            (new File(descPath)).mkdirs();
        }
        writeMoreTempArrayComputeOneToTxt(getAllMoreTempOneData,moreTempOneComputePathName);
        //这里写计算
        return AjaxResult.success();
    }

    /**
     * 导出多点位温度列表
     */
    @PreAuthorize("@ss.hasPermi('baseinfo:moreTemperatureData:export')")
    @Log(title = "多点位温度", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CzMoreTemperatureData czMoreTemperatureData)
    {
        List<CzMoreTemperatureData> list = czMoreTemperatureDataService.selectCzMoreTemperatureDataList(czMoreTemperatureData);
        ExcelUtil<CzMoreTemperatureData> util = new ExcelUtil<CzMoreTemperatureData>(CzMoreTemperatureData.class);
        util.exportExcel(response, list, "多点位温度数据");
    }

    /**
     * 获取多点位温度详细信息
     */
    @PreAuthorize("@ss.hasPermi('baseinfo:moreTemperatureData:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(czMoreTemperatureDataService.selectCzMoreTemperatureDataById(id));
    }

    /**
     * 新增多点位温度
     */
    @PreAuthorize("@ss.hasPermi('baseinfo:moreTemperatureData:add')")
    @Log(title = "多点位温度", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CzMoreTemperatureData czMoreTemperatureData)
    {
        return toAjax(czMoreTemperatureDataService.insertCzMoreTemperatureData(czMoreTemperatureData));
    }

    /**
     * 修改多点位温度
     */
    @PreAuthorize("@ss.hasPermi('baseinfo:moreTemperatureData:edit')")
    @Log(title = "多点位温度", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CzMoreTemperatureData czMoreTemperatureData)
    {
        return toAjax(czMoreTemperatureDataService.updateCzMoreTemperatureData(czMoreTemperatureData));
    }

    /**
     * 删除多点位温度
     */
    @PreAuthorize("@ss.hasPermi('baseinfo:moreTemperatureData:remove')")
    @Log(title = "多点位温度", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(czMoreTemperatureDataService.deleteCzMoreTemperatureDataByIds(ids));
    }



    public static void writeMoreTempArrayComputeOneToTxt(double[][] data, String string) {
        try {
            FileWriter fw = new FileWriter(string);
            fw.write("T1"+ "\t\t\t");
            fw.write("T2"+ "\t\t\t");
            fw.write("Q"+ "\t\t\t");
            fw.write("K"+ "\t\t\t");
            fw.write("λ"+"\t\t\t");
            fw.write("H"+ "\t\t\t");
            fw.write("Tx"+ "\t\t\t");
            fw.write("T2-T1"+ "\t\t\t");
            fw.write("\n");
            //遍历四维数组
            for (int i = 0; i <data.length ; i++) {
                for (int j = 0; j <data[0].length ; j++) {
                    fw.write(data[i][j]+ "\t\t\t");
                }
                fw.write("\n");
            }
            fw.close();
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }
}
